﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Diagnostics;
using System.Numerics;




namespace elcalc
{
    public partial class KabelForm : Form
    {
        private Kabel kabelDerRettes = null;
        DataTable dt;
        public delegate void KabelOpdateretHandler(object sender, Kabel k);
        public event KabelOpdateretHandler KabelOpdateret;

        public delegate void KabelRedigeretHandler(Kabel rettetKabel);
        public event KabelRedigeretHandler KabelRedigeret;
        

        public KabelForm()
        {
            InitializeComponent();            

            using (OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Databaser\Database4.accdb"))
            {
                try
                {
                    connection.Open();

                    DataSet dataSet = new DataSet();
                    //OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Ark5 WHERE Navn LIKE '%NOIK%'", connection);
                    OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Kabler ORDER BY Id ASC", connection);

                    adapter.Fill(dataSet);

                    //dataSet.Tables[0].Select();

                    dt = dataSet.Tables[0];
                    /*
                    foreach (DataRow row in dt.Rows)
                    {
                        cmbNavn.Items.Add(row["Navn"].ToString());
                    }*/
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    connection.Close();
                }

            }
            /*
            DataView dv = new DataView(dt);
            DataTable dtbl = dv.ToTable(true, "Type");

            foreach (DataRow dr in dtbl.Rows)
            {
                cmbType.Items.Add(dr["Type"].ToString());
            }*/

        }

        public KabelForm(Kabel k)
        {
            kabelDerRettes = k;

            InitializeComponent();

            using (OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Databaser\Database4.accdb"))
            {
                try
                {
                    connection.Open();

                    DataSet dataSet = new DataSet();
                    //OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Ark5 WHERE Navn LIKE '%NOIK%'", connection);
                    OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Kabler ORDER BY Id ASC", connection);

                    adapter.Fill(dataSet);

                    //dataSet.Tables[0].Select();

                    dt = dataSet.Tables[0];
                    /*
                    foreach (DataRow row in dt.Rows)
                    {
                        cmbNavn.Items.Add(row["Navn"].ToString());
                    }*/
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
                finally
                {
                    connection.Close();
                }

            }

            txtLængde.Text = kabelDerRettes.Længde.ToEngineering();
            cmbType.SelectedText = kabelDerRettes.Type;
            //cmbNavn.SelectedValue = k.Navn;
        }

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbNavn.Items.Clear();
            switch (cmbType.SelectedItem.ToString())
            {
                case "Cu":
                    foreach (DataRow r in dt.Select("Navn NOT LIKE '*AL' AND Navn NOT LIKE '1 G*'","Id ASC"))
                    {
                        cmbNavn.Items.Add(r["Navn"].ToString());
                    }
                    break;
                case "AL":
                    foreach (DataRow r in dt.Select("Navn LIKE '*AL'","Id ASC"))
                    {
                        cmbNavn.Items.Add(r["Navn"].ToString());
                    }
                    break;
            }
        }

        private void cmbNavn_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataRow[] dr = dt.Select("Navn = '" + cmbNavn.SelectedItem.ToString() + "'");
            txtR.Text = dr[0]["R"].ToString();
            txtX.Text = dr[0]["X"].ToString();
            RegnZ();
        }

        private void RegnZ()
        {
            
            Complex cmp = new Complex(double.Parse(txtR.Text), double.Parse(txtX.Text));
            Complex cmp20;
            txtZ.Text = string.Format(new KompleksFormat(), "{0:J2}", cmp);
            txtZ20.Text = string.Format(new KompleksFormat(), "{0:J2}", cmp20 = cmp * double.Parse(txtLængde.Text));
            txtZ120.Text = string.Format(new KompleksFormat(), "{0:J2}", new Complex(cmp20.Real*1.5, cmp20.Imaginary));
           // txtZ115.Text =  txtZ20.Text;
        }

        private void btnTilføj_Click(object sender, EventArgs e)
        {
            if (kabelDerRettes != null)
            {


                kabelDerRettes.Navn = cmbNavn.SelectedItem.ToString();
                kabelDerRettes.Længde = double.Parse(txtLængde.Text);
                kabelDerRettes.Type = cmbType.SelectedItem.ToString();

                kabelDerRettes.Z = new Complex(double.Parse(txtR.Text), double.Parse(txtX.Text));
                kabelDerRettes.Z15 = kabelDerRettes.Z * kabelDerRettes.Længde;
                kabelDerRettes.Z115 = new Complex(kabelDerRettes.Z15.Real * 1.5, kabelDerRettes.Z15.Imaginary);

                KabelRedigeret(kabelDerRettes);
//                KabelRedigeret(nodeDerRedigeres, k1);               
            }
            else
            {
                Kabel k1 = new Kabel();

                k1.Navn = cmbNavn.SelectedItem.ToString();
                k1.Længde = double.Parse(txtLængde.Text);
                k1.Type = cmbType.SelectedItem.ToString();

                k1.Z = new Complex(double.Parse(txtR.Text), double.Parse(txtX.Text));
                k1.Z15 = k1.Z * k1.Længde;
                k1.Z115 = new Complex(k1.Z15.Real * 1.5, k1.Z15.Imaginary);

                KabelOpdateret(this, k1);
            }

            Close();
        }

        private void btnAnnuller_Click(object sender, EventArgs e)
        {
            Close();
        }

    }
}
